<template>
	<view>
		<view class="text-center">
			<view style="margin-top: 30px;"><text class="text-red">￥</text><text class="text-red" style="font-size: 24px;">{{price}}</text></view>
			<view style="margin-top: 15px;" class="text-gray">共{{num}}件商品</view>
		</view>
		<view class="padding" style="margin-top: 50%;">
			<view class="padding-bottom text-grey">支付方式</view>
			<view class="solid-bottom solid-top padding flex align-center justify-between" style="background-color: #F5F5F5;">
				<view>
					<text class="cuIcon-weixin"></text><text class="padding-left">微信支付</text>
				</view>
				<text class="cuIcon-squarecheckfill"></text>
			</view>
		</view>
		<view class="padding flex flex-direction" style="position: absolute;left: 0;bottom: 30px;width: 100%;">
			<button @tap="pay" class="cu-btn lg" style="background-color: #A5673F;color: #FFFFFF;">开始支付</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				price: '',
				num: 0,
				orderno: '',
				from: '',
				
				orderindex:''
			}
		},
		onLoad(options) {
			this.price = options.price;
			this.num = options.num;
			this.orderno = options.orderno;
			this.from = options.from;
			this.orderindex = options.orderindex;//只对列表进来的有效
		},
		methods: {
			pay() {
				var that = this;
				//调用预支付接口发起支付
				this.$get('mall/pay/create', {
					orderno: this.orderno,
					appid: uni.getAccountInfoSync().miniProgram.appId
				}, 'post', true).then((res) => {
					if (res.data) {
						uni.requestPayment({
							provider: 'wxpay',
							nonceStr: res.data.nonceStr,
							signType: res.data.signType,
							paySign: res.data.paySign,
							timeStamp: res.data.timeStamp,
							package: res.data.package,
							success(pay) {
								console.log(pay)

								//通知后台支付成功
								that.$get('mall/pay/prenotify', {
									orderno: that.orderno
								}, 'post', true).then((notify) => {
									if (notify.data) {
										if (that.from == 'fillorder') {
											uni.redirectTo({
												url: '/pages/order/detail?orderno=' + that.orderno
											})
										} else {
											uni.setStorage({
												key:'order_'+that.orderno,
												data:'payed',
												success() {
													uni.navigateBack({
														delta: 1
													})
												}
											})
										}
									}
								});
							},
							fail(e) {
								console.log(e)
								uni.showToast({
									icon: 'none',
									title: '已取消支付',
									success() {
										if (that.from == 'fillorder') {
											uni.redirectTo({
												url: '/pages/order/detail?orderno=' + that.orderno
											})
										} else {
											uni.navigateBack({
												delta: 1
											})
										}
									}
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}
</style>
